﻿//
// Copyright (C) 2011 Canyala Innovation AB.
// All rights reserved.
//

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Canyala.Lagoon.Threading
{
    public class Scheduler
    {
        private Action _action;
        private DispatcherTimer _timer;

        private Scheduler(TimeSpan interval, Action action)
        {
            _timer = new DispatcherTimer();
            _timer.Interval = interval;
            _timer.Tick += _timerTick;
            _action = action;
        }

        public static Scheduler Create(TimeSpan interval, Action action)
            { return new Scheduler(interval, action); }

        private void _timerTick(object sender, EventArgs e)
            { _action(); }

        public bool IsRunning
            { get { return _timer.IsEnabled; } }

        public void Start()
            { _timer.Start(); }

        public void Stop()
            { _timer.Stop(); }
    }
}
